<script setup>
import { onMounted, ref, } from 'vue';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
const router = useRouter();
const state = ref({
  isShowView: false,
})
const good = ref();
const goodMemory = ref();
const goodColor = ref();
const memoryPicker = ref();
const colorPicker = ref();
const goodId = router.currentRoute.value.params.id;
const getGood = (id) => {
  axios({
    url: "http://124.71.66.63:9090/good/getGood",
    method: 'GET',
    params: {
      Good_id: id
    },
  }).then((res => {
    if (res.data.isSuccess == true) {
      good.value = res.data.data;
      goodMemory.value = res.data.data.Good_memory.split(',');
      goodColor.value = res.data.data.Good_color.split(',');
      state.value.isShowView = true;
      memoryPicker.value=goodMemory.value[0];
      colorPicker.value=goodColor.value[0];
    }
    else {
      ElMessage({
        message: res.data.errorMsg,
        type: 'error'
      })
      router.push('/home')
    }
  }))
}

// 将商品添加至用户购物车
const addCart = ()=> {
  console.log('memory',memoryPicker.value);
  console.log('color',colorPicker.value);
  if (memoryPicker.value != undefined && colorPicker.value != undefined ) {
    axios({
      url: 'http://124.71.66.63:9090/cart/addCart',
      method:"POST",
      data:{
        Cart_memory:memoryPicker.value,
        Cart_color:colorPicker.value,
        Cart_count:1,
        Good_id:good.value.Good_id,
        User_account:localStorage.getItem('account')
      },
      headers: { 'content-type': 'application/x-www-form-urlencoded' }
    }).then(res=>{
      if (res.data.isSuccess == true){
        ElMessage({
          message: res.data.data,
          type:'success'
        })
      }
      else{
        ElMessage({
          message: res.data.errorMsg,
          type:'error'
        })
      }
    })
  }
}

onMounted(() => {
  getGood(goodId);

})

</script>
<template>
  <div id="good" v-if="state.isShowView">
    <!-- 顶部bar -->
    <div class="header">
      <Header></Header>
    </div>
    <!-- 头部 -->
    <div class="page-header">
      <div class="title MyPage">
        <p>{{ good.Good_brand }} {{ good.Good_name }}</p>
        <div class="list">
          <ul>
            <li>
              <router-link to>概述</router-link>
              <!-- 垂直分割线 -->
              <el-divider direction="vertical" />
            </li>
            <li>
              <router-link to>参数</router-link>
              <!-- 垂直分割线 -->
              <el-divider direction="vertical" />
            </li>
            <li>
              <router-link to>用户评价</router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div id="details">
      <!-- 头部END -->

      <!-- 主要内容 -->
      <div class="main MyPage">
        <!-- 左侧商品轮播图 -->
        <div class="block">
          <el-carousel height="560px">
            <el-carousel-item>
              <img style="width: 100%" :src="good.Good_image" />
            </el-carousel-item>
          </el-carousel>
          <div>
            <img style="height:560px;" />
          </div>
        </div>
        <!-- 左侧商品轮播图END -->

        <!-- 右侧内容区 -->
        <div class="content">
          <h1 class="name">{{ good.Good_brand }} {{ good.Good_name }}</h1>
          <p class="intro ">{{ good.Good_desc }}</p>
          <p class="store">旗舰商城自营</p>
          <div class="price">
            <span>{{ good.Good_discountPrice }}元</span>
            <span class="del">{{ good.Good_price }}元</span>
          </div>
          <!-- 版本和颜色选择 -->
          <div class="buy-option">
            <el-form>
              <div class="choose-version">
                <p class="lable-p">选择版本</p>
                <el-form-item>
                  <el-radio-group v-model="memoryPicker" border>
                    <el-radio-button v-for="item in goodMemory" :label="item" :value="item" ></el-radio-button>
                  </el-radio-group>
                </el-form-item>
              </div>
              <div class="choose-color">
                <p class="lable-p">选择颜色</p>
                <el-form-item>
                  <el-radio-group v-model="colorPicker">
                    <el-radio-button v-for="item in goodColor" :label="item" :value="item" border></el-radio-button>
                  </el-radio-group>
                </el-form-item>
              </div>
              <el-form-item>
                <!-- 内容区底部加入购物车按钮 -->
                <div class="button">
                  <el-button class="add-shop-cart" @click="addCart()">加入购物车</el-button>
                  <el-button class="like">♡喜欢</el-button>
                </div>
              </el-form-item>
            </el-form>
          </div>

          <!-- 内容区底部按钮END -->
          <div class="pro-policy">
            <ul class="pro-policy-ul">
              <li>
                <el-icon>
                  <CircleCheck />
                </el-icon> 旗舰商城自营
                <!-- <i class="el-icon-circle-check"></i> 小米自营 -->
              </li>
              <li>
                <el-icon>
                  <CircleCheck />
                </el-icon> 旗舰商城发货
              </li>
              <li>
                <el-icon>
                  <CircleCheck />
                </el-icon> 7天无理由退货
              </li>
              <li>
                <el-icon>
                  <CircleCheck />
                </el-icon> 7天价格保护
              </li>
              <li>
                <el-icon>
                  <CircleCheck />
                </el-icon> 运费说明
              </li>
            </ul>
          </div>
        </div>
        <!-- 右侧内容区END -->
      </div>
      <!-- 主要内容END -->
    </div>
  </div>
</template>

<style lang="less">
#good {

  /* 头部CSS */
  .page-header {
    height: 64px;
    z-index: 4;
    background: #fff;
    border-bottom: 1px solid #e0e0e0;
    -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
    box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
  }

  .page-header .title {
    width: 1225px;
    height: 64px;
    line-height: 64px;
    font-size: 18px;
    font-weight: 400;
    color: #212121;
    margin: 0 auto;
  }

  .page-header .title p {
    float: left;
  }

  .page-header .title .list {
    height: 64px;
    float: right;
  }

  .page-header .title .list ul {
    list-style: none;
  }

  .page-header .title .list li {
    float: left;

  }

  .page-header .title .list li a {
    font-size: 14px;
    color: #616161;
  }

  .page-header .title .list li a:hover {
    font-size: 14px;
    color: #409EFF;
  }

  /* 头部CSS END */

  /* 主要内容CSS */
  #details .main {
    width: 1225px;
    height: 560px;
    padding-top: 30px;
    margin: 0 auto;
  }

  #details .main .block {
    float: left;
    width: 560px;
    height: 560px;
  }

  #details .el-carousel .el-carousel__indicator .el-carousel__button {
    background-color: rgba(163, 163, 163, 0.8);
  }

  #details .main .content {
    float: left;
    margin-left: 25px;
    width: 640px;
  }

  #details .main .content .name {
    height: 30px;
    line-height: 30px;
    font-size: 24px;
    font-weight: normal;
    color: #212121;
  }

  #details .main .content .intro {
    color: #b0b0b0;
    padding-top: 10px;
  }

  #details .main .content .store {
    color: #409EFF;
    padding-top: 10px;
  }

  #details .main .content .price {
    display: block;
    font-size: 18px;
    color: #409EFF;
    border-bottom: 1px solid #e0e0e0;
    padding: 25px 0 25px;
  }

  #details .main .content .price .del {
    font-size: 14px;
    margin-left: 10px;
    color: #b0b0b0;
    text-decoration: line-through;
  }

  .pro-policy-ul {
    list-style: none;
  }

  div.choose-version {
    margin-top: 30px;
  }

  div.choose-color {
    margin-top: 30px;
  }

  div.buy-option  {
    .el-radio-button{
      // margin: 2px;
      
    }
    .el-radio-button__inner {
      width: 300px;
    height: 50px;
    line-height: 34px;
    margin: 2px;
    border: 1px solid #DCDFE6;
    box-shadow: none;
    }
    
  }

  div.buy-option .lable-p {
    font-size: 20px;
    line-height: 40px;
  }

  .buy-option .el-form-item__label {
    font-size: 20px;
    /* 字体大小 */
  }

  .buy-option .el-checkbox-button__inner {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50px;
    /* 根据需要调整高度 */
    border: 1px solid #dcdfe6;
    /* 默认边框颜色 */
    width: 300px;
    margin-top: 10px;
    margin-left: 20px;
  }

  #details .main .content .button {
    height: 55px;
    margin: 10px 0 20px 0;
  }

  #details .main .content .button .el-button {
    float: left;
    height: 55px;
    font-size: 16px;
    color: #fff;
    border: none;
    text-align: center;
  }

  #details .main .content .button .add-shop-cart {
    width: 400px;
    background-color: #409EFF;
  }

  #details .main .content .button .add-shop-cart:hover {
    background-color: #337ecc;
  }

  #details .main .content .button .like {
    width: 200px;
    margin-left: 40px;
    background-color: #b0b0b0;
  }

  #details .main .content .button .like:hover {
    background-color: #757575;
  }

  #details .main .content .pro-policy li {
    float: left;
    margin-right: 15px;
    color: #b0b0b0;
  }

  /* 主要内容CSS END */
}
</style>